#include "beep.h"

extern sem_t beep_sem;

u_int8_t rx_data[6];

int beep_dev_init(void)
{
    int fd;
    fd = open("/sys/class/gpio/export", O_WRONLY);
    if( fd < 0 ){
        printf("beep dev export open error!\n");
        return -1;
    }
    write(fd, "19", 2);
    close(fd);
    fd = open("/sys/class/gpio/gpio19/direction", O_WRONLY);
    if( fd < 0 ){
        printf("beep dev direction open error!\n");
        return -1;
    }
    write(fd, "out", strlen("out"));
    close(fd);

    fd = open("/sys/class/gpio/gpio19/value", O_WRONLY);
    if( fd < 0 ){
        printf("beep dev value open error!\n");
        return -1;
    }

    while(1){
        sem_wait(&beep_sem);
        for(int i = 0; i<6; i++){
                printf("rx_data[%d] = %x\n", i, rx_data[i]);
        }
        if( rx_data[2] == 0x1 ){
            write(fd, "1", 1);
        }
        else if( rx_data[2] == 0x0){
            write(fd, "0", 1);
        }
        sem_post(&beep_sem);
        usleep(50*1000);
    }
    return 0;
}


